package SVN::TeamTools::Plugins::Rev;

use warnings;
use strict;
use Carp;
use Error qw(:try);

use SVN::TeamTools::Store::Config;
use SVN::TeamTools::Store::Repo;

use HTML::Template;
use CGI;
use URI::Escape;
use Data::Dumper;

my $conf;
my $logger;
BEGIN { $conf = SVN::TeamTools::Store::Config->new(); $logger = $conf->{logger}; }

sub hasAction {
	shift;
	my %args	= @_;
	my $action	= $args{action};
	return ("|dsp.rev|get.rev.detail|" =~ /\|\Q$action\E\|/);
}

sub getTemplate {
	shift;
	my %args	= @_;
	my $action	= $args{action};
	my $cgi		= $args{cgi};

	my $template = HTML::Template->new( filename => 'SVN/TeamTools/Plugins/tmpl/rev.tmpl', path => @INC );

	my $repo	= SVN::TeamTools::Store::Repo->new();
	my $p_head	= $repo->getSvnRev();
	my $p_rev;
	if ( defined $cgi->param("rev") and $cgi->param("rev") =~ /[0-9]+/ ) {
		$p_rev = $cgi->param("rev");
	} else {
		$p_rev = $repo->getSvnRev();
	}
	$template->param(p_dsprev	=> $p_rev);
	$template->param(p_head		=> $p_head);
	return $template;
}

sub getData {
	shift;
	my %args	= @_;
	my $action	= $args{action};
	my $param	= $args{param};

	my $repo	= SVN::TeamTools::Store::Repo->new();
	my $rev		= $param->{rev};
	my $svnlook	= $repo->getLook (rev => $rev);

	$logger->debug("changedlist for rev $rev:" . Dumper($svnlook->changed_hash()));

	my %result = (
		rev		=> $rev,
		author		=> $svnlook->author(),
		date		=> $svnlook->date(),
		msg		=> $svnlook->log_msg(),
		filetypes	=> $conf->{indextypes},
		changedlist	=> $svnlook->changed_hash(),
	);
	return \%result;
}
1;
